package gq.example.controller;

import gq.example.constant.MessageConstant;
import gq.example.dto.QuestionDto;
import gq.example.dto.QuestionPageQueryDto;
import gq.example.entity.Question;
import gq.example.result.PageResult;
import gq.example.result.Result;
import gq.example.service.IUsersService;
import gq.example.service.QuestionService;
import gq.example.vo.QuestionVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/question")
@Tag(name = "问题相关接口")
@Slf4j
public class QuestionController {
    @Resource
    private JavaMailSenderImpl javaMailSender;

    @Resource
    private QuestionService questionService;

    @Resource
    private IUsersService iUsersService;

    @Value("${feedback.mail.username}")
    private String sendMailer;

    /**
     * 意见填写
     * @param questionDto
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "意见填写")
    public Result addQuestion(@RequestBody QuestionDto questionDto) {
        log.info("用户 {} 添加问题", questionDto.getCreateUser());
        questionService.addQuestion(questionDto);

        //获取部门id
        Integer sectorId = questionDto.getSector_id();

        //根据部门id获取所有该部门领导的邮箱
        List<String> emails = iUsersService.getEmails(sectorId);
        String[] emailArray = emails.toArray(new String[0]);

        if(emailArray.length > 0){
            //给领导发送消息
            try{
                MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(),true);
                mimeMessageHelper.setFrom(sendMailer);
                mimeMessageHelper.setTo(emailArray);
                mimeMessageHelper.setSubject(MessageConstant.subject);
                mimeMessageHelper.setText(MessageConstant.content);

                //发送邮件
                javaMailSender.send(mimeMessageHelper.getMimeMessage());

            } catch (MessagingException e){
                e.printStackTrace();
                log.info("发送邮件失败");
            }
        }

        return Result.success("添加成功");
    }

    /**
     * 历史记录
     * @param questionPageQueryDto
     * @return
     */
    @GetMapping("/history")
    @Operation (summary = "历史记录")
    public Result<PageResult> page(QuestionPageQueryDto questionPageQueryDto) {
        log.info("分页查询");
        PageResult pageResult = questionService.historyPageQuery(questionPageQueryDto);

        return Result.success(pageResult);
    }

    /**
     * 意见查看
     * @param questionPageQueryDto
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "意见查看")
    public Result<PageResult> list(QuestionPageQueryDto questionPageQueryDto) {
        log.info("用户{} 意见查看", questionPageQueryDto.getCreateUser());

        PageResult pageResult = questionService.listPageQuery(questionPageQueryDto);
        return Result.success(pageResult);
    }

    /**
     * 评价问题
     * @param id
     * @param assess
     * @return
     */
    @PostMapping("/evaluate/{id}")
    @Operation(summary ="评价问题")
    public Result evaluateQuestion(
            @PathVariable Integer id,
            @RequestParam String assess,
            @RequestParam Integer mark
    ){
        log.info("用户 {} 评价问题",id);

        questionService.evaluateQuestion(id,assess,mark);

        return Result.success("评价成功");
    }

    /**
     * 归档问题
     * @param id
     * @return
     */
    @PostMapping("/archive/{id}")
    @Operation(summary = "归档问题")
    public Result archiveQuestion(@PathVariable Integer id){
        log.info("用户 {} 归档问题",id);

        questionService.archiveQuestion(id);

        return Result.success("归档成功");
    }

    /**
     * 修改问题
     * @param questionDto
     * @param id // 问题id
     * @return
     */
    @PutMapping("/update/{id}")
    @Operation(summary = "信息更新")
    public Result updateQuestion(@RequestBody QuestionDto questionDto,@PathVariable Integer id){
        log.info("用户 {} 修改问题",questionDto.getCreateUser());

        questionService.update(questionDto,id);

        return Result.success("修改成功");
    }

    /**
     * 回显问题 (前端回显数据)
     * @param id
     * @return
     */
    @GetMapping("/list/{id}")
    @Operation(summary = "问题查看",description = "前端回显数据")
    public Result<QuestionVo> viewQuestion(@PathVariable Integer id){
        log.info("管理员 {} 查看问题",id);

        return Result.success(questionService.viewQuestion(id));
    }

    /**
     * 获取问题内容
     * @param id
     * @return
     */
    @GetMapping("/getQuestionById/{id}")
    @Operation(summary = "获取问题内容")
    public Result<String> getQuestionById(@PathVariable Integer id){
        log.info("意见{} 被查看 ",id);

        return Result.success(questionService.getContentById(id));
    }
}
